package com.ly.honor.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ly.honor.entity.AmbitusGoods;
import com.ly.honor.entity.GoodsOrder;
import com.ly.honor.entity.User;
import com.ly.honor.entity.dto.OrderCreateDTO;
import com.ly.honor.entity.dto.OrderEditDTO;
import com.ly.honor.entity.dto.OrderQueryDTO;
import com.ly.honor.entity.vo.GoodsQueryVO;
import com.ly.honor.entity.vo.OrderQueryVO;
import com.ly.honor.exception.HonorException;
import com.ly.honor.mapper.AmbitusGoodsMapper;
import com.ly.honor.mapper.GoodsOrderMapper;
import com.ly.honor.mapper.UserMapper;
import com.ly.honor.service.GoodsOrderService;
import com.ly.honor.util.RedisUtil;
import com.ly.honor.util.ResultEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
* @author 17798
* @description 针对表【bs_goods_order】的数据库操作Service实现
* @createDate 2023-03-31 17:13:43
*/
@Service
public class GoodsOrderServiceImpl extends ServiceImpl<GoodsOrderMapper, GoodsOrder>
    implements GoodsOrderService {

    @Autowired
    private GoodsOrderMapper goodsOrderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private AmbitusGoodsMapper ambitusGoodsMapper;

    @Override
    public Page<OrderQueryVO> getOrderPage(long pageNum, long pageSize, OrderQueryDTO orderQueryDTO) {
        Page<OrderQueryVO> page = new Page<>(pageNum,pageSize);
        Page<OrderQueryVO> orderQueryVOPage  = goodsOrderMapper.getOrderPage(page,orderQueryDTO);
        for (OrderQueryVO orderQueryVO : orderQueryVOPage.getRecords()){
            var goodsId = orderQueryVO.getGoodsId();
            var ambitusGoods = ambitusGoodsMapper.selectById(goodsId);
            var goodsNum = new BigDecimal(orderQueryVO.getGoodsNum());
            orderQueryVO.setGoodsPriceTotal(ambitusGoods.getPrice().multiply(goodsNum));
        }
        return orderQueryVOPage;
    }

    @Override
    public OrderQueryVO getOrderById(Integer id) {
        OrderQueryVO orderQueryVO = goodsOrderMapper.getOrderById(id);
        return orderQueryVO;
    }

    @Override
    public String addOrder(OrderCreateDTO orderCreateDTO, HttpServletRequest request) {
        if(ObjectUtils.isEmpty(orderCreateDTO)){
            throw new HonorException(ResultEnum.OBJECT_NOT_NULL);
        }
        var order = new GoodsOrder();
        BeanUtils.copyProperties(orderCreateDTO,order);
        var orderNumber = IdUtil.simpleUUID().substring(0, 12);
        order.setOrderNumber(orderNumber);
        var token = request.getHeader("X-Token");
        var userInfo = redisUtil.get(token);
        var objectMapper = new ObjectMapper();
        var user = objectMapper.convertValue(userInfo, User.class);
        order.setUserId(user.getId());
        goodsOrderMapper.insert(order);
        return orderNumber;
    }

    @Override
    public void updateOrder(OrderEditDTO orderEditDTO) {
        if(ObjectUtils.isEmpty(orderEditDTO)){
            throw new HonorException(ResultEnum.OBJECT_NOT_NULL);
        }
        var order = new GoodsOrder();
        BeanUtils.copyProperties(orderEditDTO,order);

        goodsOrderMapper.updateById(order);
    }

    @Override
    public List<OrderQueryVO> getOrderByOrderStatus(Integer orderStatusId, HttpServletRequest request) {
        var token = request.getHeader("X-Token");
        var userInfo = redisUtil.get(token);
        var objectMapper = new ObjectMapper();
        var user = objectMapper.convertValue(userInfo, User.class);
        List<OrderQueryVO> orderQueryVOList = goodsOrderMapper.getOrderByOrderStatus(orderStatusId,user);
        for (OrderQueryVO orderQueryVO:orderQueryVOList){
            var goodsId = orderQueryVO.getGoodsId();
            var ambitusGoods = ambitusGoodsMapper.selectById(goodsId);
            var goodsNum = new BigDecimal(orderQueryVO.getGoodsNum());
            orderQueryVO.setGoodsPriceTotal(ambitusGoods.getPrice().multiply(goodsNum));
        }
        return orderQueryVOList;
    }

    @Override
    public List<OrderQueryVO> getOrderByUser(HttpServletRequest request) {
        var token = request.getHeader("X-Token");
        var userInfo = redisUtil.get(token);
        var objectMapper = new ObjectMapper();
        var user = objectMapper.convertValue(userInfo, User.class);
        List<OrderQueryVO> orderQueryVOList = goodsOrderMapper.getOrderByUser(user);
        for (OrderQueryVO orderQueryVO:orderQueryVOList){
            var goodsId = orderQueryVO.getGoodsId();
            var ambitusGoods = ambitusGoodsMapper.selectById(goodsId);
            var goodsNum = new BigDecimal(orderQueryVO.getGoodsNum());
            orderQueryVO.setGoodsPriceTotal(ambitusGoods.getPrice().multiply(goodsNum));
        }
        return orderQueryVOList;
    }

    @Override
    public void updateOrderStatus(Integer id, Integer orderStatusId) {
        var order = goodsOrderMapper.selectById(id);
        if(ObjectUtils.isEmpty(order)){
            throw new HonorException(ResultEnum.MESSAGE_INVALID);
        }
        order.setOrderStatusId(orderStatusId);
        goodsOrderMapper.updateById(order);
    }
}




